using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WMSSystem.Application.Contracts.Workflow;
using WMSSystem.Application.Contracts.Workflow.Dto;

namespace WMSSystem.HttpApi.Controllers
{
    /// <summary>
    /// 工作流控制器
    /// </summary>
    [Route("api/app/[controller]")]
    [ApiController]
    public class WorkflowController : ControllerBase
    {
        private readonly IWorkflowService _workflowService;

        public WorkflowController(IWorkflowService workflowService)
        {
            _workflowService = workflowService;
        }

        /// <summary>
        /// 创建工作流实例
        /// </summary>
        /// <param name="input">创建输入</param>
        /// <returns>工作流实例ID</returns>
        [HttpPost("create")]
        public async Task<ApiResponse<string>> CreateWorkflowInstance([FromBody] CreateWorkflowInstanceDto input)
        {
            return await _workflowService.CreateWorkflowInstanceAsync(input);
        }

        /// <summary>
        /// 审批工作流
        /// </summary>
        /// <param name="input">审批输入</param>
        /// <returns>审批结果</returns>
        [HttpPost("approve")]
        public async Task<ApiResponse<bool>> ApproveWorkflow([FromBody] WorkflowApprovalDto input)
        {
            return await _workflowService.ApproveWorkflowAsync(input);
        }

        /// <summary>
        /// 获取工作流实例列表
        /// </summary>
        /// <returns>工作流实例列表</returns>
        [HttpGet("instances")]
        public async Task<ApiResponse<System.Collections.Generic.List<WorkflowInstanceDto>>> GetWorkflowInstances()
        {
            return await _workflowService.GetWorkflowInstancesAsync();
        }

        /// <summary>
        /// 获取工作流实例
        /// </summary>
        /// <param name="workflowInstanceId">工作流实例ID</param>
        /// <returns>工作流实例</returns>
        [HttpGet("instance/{workflowInstanceId}")]
        public async Task<ApiResponse<WorkflowInstanceDto>> GetWorkflowInstance(string workflowInstanceId)
        {
            return await _workflowService.GetWorkflowInstanceAsync(workflowInstanceId);
        }

        /// <summary>
        /// 获取工作流状态
        /// </summary>
        /// <param name="workflowInstanceId">工作流实例ID</param>
        /// <returns>工作流状态</returns>
        [HttpGet("status/{workflowInstanceId}")]
        public async Task<ApiResponse<object>> GetWorkflowStatus(string workflowInstanceId)
        {
            return await _workflowService.GetWorkflowStatusAsync(workflowInstanceId);
        }

        /// <summary>
        /// 获取业务相关的工作流实例
        /// </summary>
        /// <param name="businessType">业务类型</param>
        /// <param name="businessId">业务ID</param>
        /// <returns>工作流实例</returns>
        [HttpGet("business/{businessType}/{businessId}")]
        public async Task<ApiResponse<WorkflowInstanceDto>> GetWorkflowInstanceByBusiness(string businessType, Guid businessId)
        {
            return await _workflowService.GetWorkflowInstanceByBusinessAsync(businessType, businessId);
        }

        /// <summary>
        /// 获取待审批列表
        /// </summary>
        /// <returns>待审批列表</returns>
        [HttpGet("pending-approvals")]
        public async Task<ApiResponse<System.Collections.Generic.List<WorkflowInstanceDto>>> GetPendingApprovals()
        {
            return await _workflowService.GetPendingApprovalsAsync();
        }

        /// <summary>
        /// 获取待审批列表
        /// </summary>
        /// <param name="approverId">审批人ID</param>
        /// <returns>待审批列表</returns>
        [HttpGet("pending/{approverId}")]
        public async Task<ApiResponse<System.Collections.Generic.List<WorkflowInstanceDto>>> GetPendingApprovalList(Guid approverId)
        {
            return await _workflowService.GetPendingApprovalListAsync(approverId);
        }

        /// <summary>
        /// 获取我的审批记录
        /// </summary>
        /// <param name="approverId">审批人ID</param>
        /// <returns>审批记录列表</returns>
        [HttpGet("history/{approverId}")]
        public async Task<ApiResponse<System.Collections.Generic.List<WorkflowInstanceDto>>> GetMyApprovalHistory(Guid approverId)
        {
            return await _workflowService.GetMyApprovalHistoryAsync(approverId);
        }
    }
} 